$(function() {
	question_upload_img.init();
	
	
});

var question_upload_img = {
		
		init: function init(){
			this.prepareUploadify();
			this.prepareDialog();
			this.bindDisplayImg();
		},
		
		drawModal : function drawModal(){

		},
		
		prepareDialog : function prepareDialog() {
			$("#question-add-form").on("click",".add-img",function() {
				
				$(".fade").modal({backdrop:true,keyboard:true});
				
//				$("#add-question-img-dialog").dialog("open");
				$("#file-name").empty();
				
				if($(this).hasClass("add-content-img")){
					$(".img-destination label").text("试题内容");
					$(".img-destination input").val(-1);
				}else if($(this).hasClass("add-opt-img")){
					$(".img-destination label").text("试题选项 ");
					var this_index = $(".add-opt-img").index($(this));
					$(".img-destination label").append(String.fromCharCode(65 + this_index));
					$(".img-destination input").val(this_index);
				}
			});
		},
		prepareUploadify : function prepareUploadify(){
			$("#uploadify").uploadifive({
				
				'debug'	 : false,
				'buttonText'	: '点击上传',
				'uploadScript'	 : document.getElementsByTagName('base')[0].href + 'secure/upload-uploadify-img/',
				'multi'	 : false,
				'auto'	 : true,
				'height'	 : '26',
				'width'	 : '60',
				'fileSizeLimit'	: '100', // expects input in kb
                'fileType': 'image/*',
                'uploadLimit'  : 2,
                'queueSizeLimit' : 1,
                'cancelImage'	: document.getElementsByTagName('base')[0].href + 'resources/js/uploadifive/uploadifive-cancel.png',
                'overrideEvents' :['onError','onFallback','onUploadComplete'],
                'allowFileType'   : ['png','jpg','gif'],
                'onFallback' : function () { //如果浏览器没有兼容的HTML5文件API功能，则在初始化期间触发
                    util.error('浏览器不支持HTML5,无法上传！');
                },
                'onCancel': function(file){
                    /* 注意：取消后应重新设置uploadLimit */
                    var $data    = $(this).data('uploadifive'),
                        settings = $data.settings;
                    settings.uploadLimit++;
                },
                'onUploadComplete': function (file, data) {   //对于完成的每个文件上载，触发一次。
                    $('#maincontent').load(location.href+' #maincontent > *');
                    $(".fade").modal('hide');
                    if (data) {
                        var fileurl = data;
                        var destination  = $(".img-destination input").val();
                        if(destination == -1){
//			        		var textareaval = $(".question-content textarea").val();
//			        		$(".question-content textarea").val( textareaval + "<img class=\"question-content-img\" src=\"" +  fileurl + "\">");

                            var displayImg = $(".question-content").find(".diaplay-img");
                            if(displayImg.length  == 0){
                                $(".question-content textarea").after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-img display-content-img\" target=\"_blank\" data-url=\"" + fileurl + "\">预览图片</a>");
                            }else{
                                displayImg.attr("href", fileurl);
                            }

                        }else{
                            var thisopt =  $($(".add-opt-item")[destination]);
                            var displayImg = thisopt.find(".diaplay-img");

                            if(displayImg.length  == 0){
                                thisopt.find("input.form-question-opt-item").after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-img display-opt-img\" target=\"_blank\" data-url=\"" + fileurl + "\">预览图片</a>");
                            }else{
                                displayImg.attr("href", fileurl);
                            }

//			        		var optval = thisopt.val();
//			        		thisopt.val(optval + "<img class=\"question-opt-img\" src=\"" +  fileurl + "\">");
                        }

                        /*$("#add-question-img-dialog").dialog( "close" );*/
                        util.success("上传成功");
                    }
                },
                'onError': function (errorType, file, uploadAll) {
                    switch (errorType) {
                        case 'UPLOAD_LIMIT_EXCEEDED':
                            util.error("上传的文件数量已经超出系统限制数量！");
                            break;
                        case 'FILE_SIZE_LIMIT_EXCEEDED':
                            util.error("文件大小超出系统限制的100kb！");
                            break;
                        case 'QUEUE_LIMIT_EXCEEDED':
                            util.error("任务数量超出队列限制");
                            break;
                        case 'FORBIDDEN_FILE_TYPE':
                            util.error("文件类型不正确！");
                            break;
                        case '404_FILE_NOT_FOUND':
                            util.error("文件未上传成功或服务器存放文件的文件夹不存在!");
                            break;
                    }
                }
			});
		},
		
		bindDisplayImg : function bindDisplayImg(){
			$("#bk-conent-question-content").delegate(".diaplay-img","click",function() {
				window.open(location.protocol + "//" + location.host + "/" + $(this).attr("href"));
				e.preventDefault();
			});
		}
		
};